<?php
namespace app\api\controller;

use think\Controller;
use think\Db;

class Lideta extends Controller
{
    // 编码
    public function __construct()
    {
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:GET, POST, OPTIONS');
    }
    
    //首页查询数据
    public function userdata()
    {
//         $res = Db::query(' select
//         housedetails.house_id,
//         housedetails.house_name,
//         housedetails.house_size,
//         housedetails.house_des,
//         housedetails.house_state,
//         housedetails.house_price,
//         housedetails.house_type,
//         housedetails.house_direction,
//         houseimg.img1,
//         houseimg.img2,
//         houseimg.img3,
//         houseimg.img4,
//         houseimg.img5,
//         houseimg.house_id
//         from
//         housedetails right join houseimg
//         on housedetails.house_id=houseimg.house_id');
        $res = Db::table("housedetails")->alias("a")
            ->join('houseimg i', 'a.house_id=i.house_id')
            ->field('a.house_id,a.house_name,a.house_type,a.house_size,a.house_des,a.house_state,a.house_price,a.house_direction,i.img1')
            ->paginate(3);
        return json($res);
    }

    //模糊查询
    public function sellike()
    {
        //得到用户查询胡条件进行模糊查询
        $name = $_POST['userval'];
        $res = Db::table('housedetails')->alias('a')
            ->join('houseimg w', 'a.house_id = w.house_id', 'INNER')
            ->where("a.house_size", "like", "%$name%")
            ->whereOr("a.house_name", "like", "%$name%")
            ->whereOr("a.house_des", "like", "%$name%")
            ->whereOr("a.house_type", "like", "%$name%")
            ->whereOr("a.house_price", "like", "%$name%")
            ->whereOr("a.house_direction", "like", "%$name%")
            ->whereOr("a.house_wintype", "like", "%$name%")
            ->whereOr("a.house_state", "like", "%$name%")
            ->whereOr("a.house_direction", "like", "%$name%")
           ->paginate(3);
        /*1、判断根据用户输入的条件是否查询到数据
         * 2、若用户数据库查询到数据则返回数据
         * 3、数据库中没有查询到数据则查询全部的数据
         */
        if (count($res) > 0) {
            return json($res);
        } else {
            $res = Db::table("housedetails")->alias("a")
                ->join('houseimg i', 'a.house_id=i.house_id')
                ->field('a.house_price,a.house_type,a.house_id,a.house_lontiude,a.house_latiude,a.house_size,a.house_direction,a.house_wintype,a.house_des,i.img1')
                ->paginate(3);
            return json($res);
        }
    }

    public function finds()
    {
        $res = Db::table("housedetails")->alias("a")
            ->join('houseimg i', 'a.house_id=i.house_id')
            ->field('a.house_price,a.house_type,a.house_id,a.house_lontiude,a.house_latiude,a.house_size,a.house_direction,a.house_wintype,a.house_des,i.img1')
            ->paginate(3);
        return json($res);
    }
}